@namespace Oqtane.Modules.Admin.Jobs
@inherits ModuleBase
@inject IJobLogService JobLogService
@inject IStringLocalizer<Log> Localizer
@inject IStringLocalizer<SharedResources> SharedLocalizer

@if (_jobLogs == null)
{
    <p><em>@SharedLocalizer["Loading"]</em></p>
}
else
{
    <Pager Items="@_jobLogs">
        <Header>
            <th>@SharedLocalizer["Name"]</th>
            <th>@SharedLocalizer["Status"]</th>
            <th>@Localizer["Started"]</th>
            <th>@Localizer["Finished"]</th>
        </Header>
        <Row>
            <td>@context.Job.Name</td>
            <td>@DisplayStatus(context.Job.IsExecuting, context.Succeeded)</td>
            <td>@context.StartDate</td>
            <td>@context.FinishDate</td>
        </Row>
        <Detail>
            <td colspan="4">@((MarkupString)context.Notes)</td>
        </Detail>
    </Pager>
}

@code {
    private List<JobLog> _jobLogs;

    public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Host;

    protected override async Task OnParametersSetAsync()
    {
        _jobLogs = await JobLogService.GetJobLogsAsync();

        if (PageState.QueryString.ContainsKey("id"))
        {
            _jobLogs = _jobLogs.Where(item => item.JobId == Int32.Parse(PageState.QueryString["id"])).ToList();
        }

        _jobLogs = _jobLogs.OrderByDescending(item => item.JobLogId).ToList();
    }

    private string DisplayStatus(bool isExecuting, bool? succeeded)
    {
        var status = string.Empty;
        if (isExecuting)
        {
            status = Localizer["Executing"];
        }
        else
        {
            if (succeeded != null && succeeded.Value)
            {
                status = Localizer["Succeeded"];
            }
            else
            {
                status = Localizer["Failed"];
            }
        }

        return status;
    }
}
